<?php
class Tools{

    protected static $sendCodeUrl = "http://47.244.186.74:2005/message/sms/siteV5";

    public static function dd($data)
    {
        $data = is_string($data) ? $data : json_encode($data,JSON_UNESCAPED_UNICODE);
        $time = date('Y-m-d H:i:s');
        echo "[$time]$data".PHP_EOL;
    }

    //获取一个验证码
    public static function getCode($bit = 6): string
    {
        $code = "";
        for ($i = 1; $i <= 6; $i ++){
            $code .= mt_rand(0,9);
        }
        return $code;
    }

    //发短信
    public static function sendCode($phone,$code = 888888,$region = 86,$ttl = 900)
    {
        $time = $ttl / 60;
        $data = [
            'region' => $region,
            'phone'  => $phone,
            'contentNo' => 'LOGIN_CODE',
            'contentParamMapStr' => "code,{$code},time,{$time}"
        ];
        $res = file_get_contents(self::$sendCodeUrl.'?'.http_build_query($data));
        if(!$res || json_decode($res,1)['code'] != 200) throw new ApiError("短信发送失败");
    }

}


